import scrapy
from scrapy_dangdang.items import ScrapyDangdangItem


class DangSpider(scrapy.Spider):
    name = 'dang'
    allowed_domains = ['category.dangdang.com']
    start_urls = ['http://category.dangdang.com/cp01.01.02.00.00.00.html']
    base_url = 'http://category.dangdang.com/pg'
    page = 1
    def parse(self, response):
#       pipelines 下载数据
#       items   定义数据结构
        li_list = response.xpath('//ul[@id="component_59"]/li')

        for li in li_list:
            # 第一张图片和其他图片的属性标签是不同的
            src = li.xpath('.//img/@data-original').extract_first()
            if src:
                src = src
            else:
                src = li.xpath('.//img/@src').extract_first()
            name = li.xpath('.//img/@alt').extract_first()
            price = li.xpath('.//p[@class="price"]/span[1]/text()').extract_first()

            book = ScrapyDangdangItem(src=src,name=name,price=price)
            # 获取一个book就将book交给pipelines
            yield book

            if self.page<10:
                self.page=self.page+1

                url = self.base_url+str(self.page)+'-cp01.01.02.00.00.00.html'
                # 怎么调用parse方法
                # url是请求地址，callback是你要执行的函数
                yield scrapy.Request(url=url,callback=self.parse)